using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;

namespace Zune.Forms
{
    public static class Utilities
    {
        internal static bool IsLocationVisible(Vector2 location, Vector2 controlLocation, Vector2 controlSize)
        {
            return IsControlInsideTouched(location, controlLocation, controlSize, 1);
        }

        public static bool IsControlTouched(Vector2 touchPosition, Vector2 controlLocation, Vector2 controlSize)
        {
            if (touchPosition.X >= controlLocation.X && touchPosition.X <= controlLocation.X + controlSize.X &&
                touchPosition.Y >= controlLocation.Y && touchPosition.Y <= controlLocation.Y + controlSize.Y)
                return true;

            return false;
        }

        internal static bool IsControlClicked(TouchLocation touch, Vector2 controlLocation, Vector2 controlSize)
        {
            if (IsControlTouched(touch.Position, controlLocation, controlSize))
            {
                if (touch.Pressure >= Constants.CLICK_PRESSURE)
                    return true;
            }

            return false;
        }

        internal static bool IsControlInsideTouched(Vector2 touchPosition, Vector2 controlLocation, Vector2 controlSize, int offset)
        {
            if (touchPosition.X >= controlLocation.X - offset && touchPosition.X <= controlLocation.X + controlSize.X - offset &&
                touchPosition.Y >= controlLocation.Y - offset && touchPosition.Y <= controlLocation.Y + controlSize.Y - offset)
                return true;

            return false;
        }

        internal static bool IsControlContextMenuCalled(GameTime pressTimeStart, GameTime pressTimeNow)
        {
            if (pressTimeStart.TotalGameTime.Add(pressTimeNow.TotalGameTime).Seconds >= 1)
                return true;

            return false;
        }

        internal static Vector2 CenterTextVector2(SpriteFont font, string text, Vector2 location, Vector2 size)
        {
            Vector2 fontSize = font.MeasureString(text);
            return new Vector2(location.X + (size.X / 2) - fontSize.X / 2, location.Y + (size.Y / 2) - fontSize.Y / 2);
        }

        internal static Vector2 CenterHorizontalTextVector2(SpriteFont font, string text, Vector2 location, Vector2 size)
        {
            Vector2 fontSize = font.MeasureString(text);
            return new Vector2(location.X + (size.X / 2) - fontSize.X / 2, location.Y);
        }

        internal static List<string> GetLinesFromString(SpriteFont font, string text, Vector2 location, Vector2 size)
        {
            List<string> lines = new List<string>();

            if (!string.IsNullOrEmpty(text))
            {
                Vector2 textSize = font.MeasureString(text);

                float width = size.X - 4;

                if (textSize.X > width)
                {
                    string line = string.Empty;
                    string[] lns = text.Split(' ');
                    //MatchCollection matches = Regex.Matches(text, @"\w+");
                    foreach (string ln in lns)
                    {
                        string tmpLine = line;
                        if (string.IsNullOrEmpty(tmpLine))
                            tmpLine = ln;
                        else
                            tmpLine += " " + ln;

                        if (font.MeasureString(tmpLine).X > width)
                        {
                            lines.Add(line + " ");
                            line = ln;
                        }
                        else
                        {
                            line = tmpLine;
                        }
                    }

                    lines.Add(line);
                }
                else
                {
                    lines.Add(text);
                }
            }

            return lines;
        }

        internal static void SaveFile(StorageDevice storageDevice, string fName, string folder, string contents)
        {
            using (StorageContainer container = storageDevice.OpenContainer(fName))
            {
                using (StreamWriter writer = new StreamWriter(Path.Combine(container.Path, fName)))
                {
                    writer.WriteLine(contents);
                }
            } 
        }

        internal static string GetFile(StorageDevice storageDevice, string fName, string folder)
        {
            string ret = string.Empty;
            using (StorageContainer container = storageDevice.OpenContainer(fName))
            {
                using (StreamReader reader = new StreamReader(Path.Combine(container.Path, fName)))
                {
                    ret = reader.ReadToEnd();
                }
            }

            return ret;
        }
    }
}